package cn.tedu.coolshark.Controller;
import cn.tedu.coolshark.mapper.ProductMapper;
import cn.tedu.coolshark.pojo.dto.ProductDTO;
import cn.tedu.coolshark.pojo.dto.ProductUpdateDTO;
import cn.tedu.coolshark.pojo.entity.Product;
import cn.tedu.coolshark.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * @author 作者：HeShaoShuai
 * @Email 邮箱：2569819541@qq.com
 * @Date 日期：2023/5/19 14:38
 * @Package 包名：cn.tedu.coolshark.Controller
 * @Project 项目名：JavaSE
 * @Version 1.0
 */
@RestController
@RequestMapping("/product/")
public class ProductController {
	@Value("${imagesPath}")
	private String dirPath;

	@Autowired(required = false)
	ProductMapper mapper;
	@RequestMapping("insert")
	public void insert(@RequestBody ProductDTO productDTO){
		System.out.println("productDTO = " + productDTO);
		Product p = new Product();
		BeanUtils.copyProperties(productDTO,p);
		p.setCreated(new Date());
		mapper.insert(p);
	}
	@RequestMapping("selectAdmin")
	public List<ProductAdminVO> selectAdmin(){
		return mapper.selectAdmin();
	}
	@RequestMapping("delete")
	public void delete(int id){

		//通过id查询图片路径 并删除文件
		String url = mapper.selectUrlById(id);
		new File(dirPath+url).delete();
		//删除数据库中数据
		mapper.deleteById(id);

	}
	@RequestMapping("selectUpdateById")
	public ProductUpdateVO selectUpdateById(int id){
		return mapper.selectUpdateById(id);
	}
	@RequestMapping("update")
	public void update(@RequestBody ProductUpdateDTO productUpdateDTO){
		System.out.println("productUpdateDTO = " + productUpdateDTO);
		//得到原来的图片路径
		String url = mapper.selectUrlById(productUpdateDTO.getId());
		//判断如果新路径和原来路径不相等则删除原来路径对应的图片
		if (!url.equals(productUpdateDTO.getUrl())){
			new File(dirPath+url).delete();
		}
		Product product = new Product();
		BeanUtils.copyProperties(productUpdateDTO,product);

		mapper.update(product);
	}
	@RequestMapping("selectTop")
	public List<ProductTopVO>selectTop(){

		return mapper.selectTop();
	}
	@RequestMapping("selectIndex")
	public List<ProductIndexVO> selectIndex(){
		return mapper.selectIndex();
	}
	//  id=5
	@RequestMapping("selectDetailById")
	public ProductDetailVO selectDetailById(int id, HttpSession session){
		//第一次从Session里面通过id获取时是获取不到内容的
		String info = (String) session.getAttribute("view"+id);
		if (info==null){//info为null代表之前没有浏览过
			//让商品的浏览量+1
			mapper.updateViewCountById(id);
			//把浏览过的id保存到Session里面去
			session.setAttribute("view"+id,"visited");
		}
		return mapper.selectDetailById(id);
	}

	@RequestMapping("selectByCid")
	public List<ProductIndexVO> selectByCid(int id){
		return mapper.selectByCid(id);
	}
	@RequestMapping("selectByWd")
	public List<ProductIndexVO> selectByWd(String wd){
		return mapper.selectByWd(wd);
	}
}
